package fr.insta.browser.view.frame;

import fr.insta.browser.view.menu.IJMenuBarWebView;
import fr.insta.webview.object.IWebObject;
import fr.insta.webview.ui.util.TableColumnIcon;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * <p>WebObjectFrame class.</p>
 *
 * @author Maarek
 * @version $Id: $Id
 */
public class WebObjectFrame extends JFrame implements MouseListener
{
	private static final long serialVersionUID = 7823932625200967377L;
	private final List<IWebObject> listWebObject;
	private final IJMenuBarWebView menuBar;
	
	String workingDir, fileExtension;
	
	/**
	 * <p>Constructor for WebObjectFrame.</p>
	 *
	 * @param _menuBar a {@link fr.insta.browser.view.menu.IJMenuBarWebView} object.
	 * @param _listWebObject a {@link java.util.List} object.
	 */
	public WebObjectFrame(final IJMenuBarWebView _menuBar, final List<IWebObject> _listWebObject)
	{
		this.menuBar = _menuBar;
		this.listWebObject = _listWebObject;
		this.generateTableEmploye();
		this.miseEnPage();
	}
	
	private void miseEnPage()
	{
		this.setSize(new Dimension(800,500));
		//this.setTitle(_title);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	/**
	 * <p>setFileInfo.</p>
	 *
	 * @param _workingDir a {@link java.lang.String} object.
	 * @param _fileExtension a {@link java.lang.String} object.
	 */
	public void setFileInfo(String _workingDir, String _fileExtension) {
		workingDir = _workingDir;
		fileExtension = _fileExtension;
	}
	
	JScrollPane scrollTableWebObject;
	DefaultTableModel tableModelWebObject;
	JTable tableWebObject;
	
	private void generateTableEmploye()
	{
		String[] nomColonnes = { "Nom", "Adresse", "" };

		tableModelWebObject = new DefaultTableModel(null, nomColonnes)
		{
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int _row, int _column)
			{
				return false;
			}
		};

		tableWebObject = new JTable(tableModelWebObject);
		tableWebObject.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableWebObject.getTableHeader().setReorderingAllowed(false);

		tableWebObject.getColumnModel().getColumn(2).setMaxWidth(20);
		tableWebObject.getColumnModel().getColumn(2).setMinWidth(20);
		tableWebObject.setRowHeight(20);
		
		tableWebObject.addMouseListener(this);

		TableColumn colCroix = tableWebObject.getColumnModel().getColumn(2);
		colCroix.setCellRenderer(new TableColumnIcon("Resources/img/delete.png"));
		
		if(listWebObject != null) {
			for (IWebObject iWebObject : listWebObject)
			{
				String[] dataRow = new String[3];
				dataRow[0] = iWebObject.getTitle();
				dataRow[1] = iWebObject.getUrl();
				dataRow[2] = "";
				tableModelWebObject.addRow(dataRow);
			}
		}
		
		scrollTableWebObject = new JScrollPane(tableWebObject);
		add(scrollTableWebObject);
	}
	

	/** {@inheritDoc} */
	@Override
	public void mouseClicked(MouseEvent e)
	{
		Point p = e.getPoint();
		int colonne = tableWebObject.columnAtPoint(p);
		int ligne = tableWebObject.rowAtPoint(p);
		
		if(colonne == 2) {
			
			IWebObject webObject = listWebObject.get(ligne);

			int reply = JOptionPane.showConfirmDialog(null, "Voulez vous vraiment supprimer ce web object ?", "Supprimer ?", JOptionPane.YES_NO_OPTION);
			if (reply == JOptionPane.YES_OPTION)
			{
				JOptionPane.showMessageDialog(null, "Ce web object a bien été supprimé !");
				
				tableModelWebObject.removeRow(ligne);
				listWebObject.remove(webObject);
				
				File webObjectFile = new File(workingDir + webObject.getDate().getTime() + fileExtension);
				
				if(webObjectFile.exists()){
					webObjectFile.delete();
				}
				
				menuBar.refresh();
			}
		}
	}
	

	/** {@inheritDoc} */
	@Override
	public void mousePressed(MouseEvent e)
	{
	}

	/** {@inheritDoc} */
	@Override
	public void mouseReleased(MouseEvent e)
	{
	}

	/** {@inheritDoc} */
	@Override
	public void mouseEntered(MouseEvent e)
	{
	}

	/** {@inheritDoc} */
	@Override
	public void mouseExited(MouseEvent e)
	{
	}

}
